package collection;

import java.util.Stack;

/**
 * 两个栈构成一个FIFO队列
 * @Author Antony
 * @Since 2018/6/17 17:33
 */
public class FIFOQueue<E>{

    private int size;

    private Stack<E> inStack;

    private Stack<E> outStack;

    public int size(){
        return size;
    }

    public FIFOQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public boolean put(E e) {
        inStack.push(e);
        size++;
        return true;
    }

    public E take() {
        if(outStack.size() == 0){
            removeInToOut();
        }
        size--;
        return outStack.size()==0 ? null : outStack.pop();
    }

    void removeInToOut(){
        while(inStack.size() != 0){
            E value = inStack.pop();
            outStack.push(value);
        }
    }
}
